app.controller('viewgoods', ['$scope', '$modal', 'requestdata', '$state', '$timeout', function($scope, $modal, requestdata, $state, $timeout) {
    $("#cover").on("change", function(event) {
        var files = event.target.files;

        $.each(files, function(index, file) {
            var reader = new FileReader();
            reader.onload = function(event) {
                $("#coverImg").attr("src", event.target.result);
            }
            reader.readAsDataURL(file);
        });
    });

    // 取消添加课程
    $scope.cancelAdd = function() {
        swal({
                title: "确定取消添加该课程？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#619ce1",
                confirmButtonText: "确定",
                closeOnConfirm: true
            },
            function() {
                $state.go("fullScore.course");
            });
    }

    $scope.openCourseInfo = function() {
        $scope.courseInfo = true;
    }
}]);

app.controller('addCourseUpload', ['$scope', '$modal', '$http', '$rootScope', '$state', '$timeout', function($scope, $modal, $http, $rootScope, $state, $timeout) {
    $scope.hideBtn = true;

    // 确定添加课时
    $scope.sureAddCourse = function(index, event) {
        var $table = $(".table-striped").eq(index);
        var $hash = $table.find(".info").find(".hash");
        var href = $hash.prev().find("a").attr("href");
        var $progress = $table.find(".status.text-left");

        if (href) {
            $scope.href = href;
        }

        if (!$scope.courseHourName) {
            swal("请填写课时名称");
            return;
        } else if ($progress.css("display") == "block") {
            swal("视频还在上传中");
            return;
        } else if (!$scope.courseHourSort) {
            swal("请填写课时排序");
            return;
        } else {
            var data = {
                course_id: $scope.courseId,
                course_hour_name: $scope.courseHourName,
                url: $scope.href,
                sort: $scope.courseHourSort
            }
            if ($scope.courseHourId) {
                data.course_hour_id = $scope.courseHourId;
                var url = "Course/editCourseHour";
            } else {
                var url = "Course/addCourseHour";
            }
            $http({
                url: $rootScope.baseUrl + url,
                method: "post",
                data: data
            }).then(function(res) {
                if (res.data.code == 200) {
                    if (!$scope.courseHourId) {
                        $scope.courseHourId = res.data.data.course_hour_id;
                    }

                    var $td = $(event.target).closest("td");
                    var $infoCourse = $td.find("p.infoCourse");
                    var $bigDiv = $td.find(".bigDiv");
                    $.each($infoCourse, function(index, ele) {
                        var $input = $(ele).find("input");

                        var $span = $(ele).find("span");
                        $input.hide();
                        $bigDiv.hide();
                        $span.show();
                        $scope.hideBtn = false;
                    });

                    $scope.courseHourList = [];
                    var info = {
                        name: $scope.courseHourName,
                        link: $scope.href,
                        sort: $scope.courseHourSort
                    }
                    $scope.courseHour[index] = info;
                } else if (res.data.code == 409) {
                    $state.go("access.signin");
                }
            }, function(error) {
                console.log(error);
            });
        }
    }

    // 编辑课时
    $scope.courseHourList = [];
    $scope.editCourseHour = function(index, event) {
        if (!$scope.courseHourId) {
            sweetAlert("编辑失败", "课时还没保存", "error");
        } else if (!$scope.hideBtn) {
            var $td = $(event.target).closest("td").prev();
            var $infoCourse = $td.find("p.infoCourse");
            var $bigDiv = $td.find(".bigDiv");

            $.each($infoCourse, function(index, ele) {
                var $input = $(ele).find("input");

                var $span = $(ele).find("span");

                $input.show();
                $bigDiv.remove();
                $span.hide();
                $scope.hideBtn = true;
            });
            $scope.courseHourList.length = 1;
        }
    }

    // 删除课时
    $scope.deleteCourseHour = function(index, event) {
        if ($scope.courseHourId) {
            swal({
                    title: "确定删除该课时？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#619ce1",
                    confirmButtonText: "确定",
                    closeOnConfirm: true
                },
                function() {
                    $http({
                        url: $rootScope.baseUrl + "Course/delCourseHour",
                        method: "post",
                        data: {
                            course_id: $scope.courseId,
                            course_hour_id: $scope.courseHourId,
                        }
                    }).then(function(res) {
                        if (res.data.code == 200) {
                            swal("刪除成功", "", "success");
                            $timeout(function() {
                                $scope.courseHour.splice(index, 1);
                                if ($scope.courseHour.length == 0 && $scope.videoList.length == 0) {
                                    $rootScope.noData = true;
                                }
                            }, 30);

                            var $td = $(event.target).closest("td").prev();
                            var $infoCourse = $td.find("p.infoCourse");
                            var $bigDiv = $td.find(".bigDiv");

                            $.each($infoCourse, function(index, ele) {
                                var $input = $(ele).find("input");

                                var $span = $(ele).find("span");

                                $input.show();
                                $bigDiv.remove();
                                $span.hide();
                                $scope.hideBtn = true;
                            });
                            $scope.courseHourList.length = 1;
                            $scope.courseHourName = "";
                            $scope.href = "";
                            $scope.courseHourSort = "";
                            $scope.courseHourId = "";
                        } else if (res.data.code == 409) {
                            $state.go("access.signin");
                        }
                    }, function(error) {
                        console.log(error);
                    });
                });
        } else {
            sweetAlert("课时还没保存", "不需要删除", "error");
        }
    }

    $scope.getRandom = function(num) {
        var str = "";
        var strNum = "abcdefghilklmnopqrstuvwxyz";
        for (var i = 0; i < num; i++) {
            str += strNum.substr(parseInt(Math.random() * strNum.length), 1);
        }
        return str;
    }
    $scope.randomStr = $scope.getRandom(5);
}]);